﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EzyChoice
{

    public partial class CriteriaWeight : Form
    {
        List<Criteria> P1 = new List<Criteria>();
        List<Criteria> P2 = new List<Criteria>();
        List<double> PX = new List<double>();
        double[,] Matrix = new double[1,1];
        Dictionary<Criteria, double> WW = new Dictionary<Criteria, double>();
        List<Criteria> Cri = new List<Criteria>();
        double[] RI = new double[] { 0.00, 0.00, 0.00, 0.58, 0.90, 1.12, 1.24, 1.32, 1.41, 1.45, 1.49, 1.51, 1.48, 1.56, 1.57, 1.59 };
        double[] size = new double[] { 0 };
        Participate Par = new Participate();

        Dictionary<Criteria, Dictionary<Criteria, double>> dicPair = new Dictionary<Criteria, Dictionary<Criteria, double>>();

        TreeView Tree = new TreeView();

        public CriteriaWeight()
        {
            InitializeComponent();
        }

        private void CriteriaWeight_Load(object sender, EventArgs e)
        {

        }

        public void Begin(EzcItem parrent, Participate par, TreeView tree)
        {
            try
            {
                Par = par;
                Tree = tree;

                List<Criteria> cs = new List<Criteria>();
                foreach (Criteria c in parrent.Childs)
                {
                    cs.Add(c);
                }

                Matrix = new double[cs.Count, cs.Count];

                lstPair.Items.Clear();
                P1.Clear();
                P2.Clear();
                WW.Clear();
                Cri.Clear();
                dicPair.Clear();

                double sum = 0;

                for (int i = 0; i < cs.Count; i++)
                {
                    WW.Add(cs[i], cs[i].weight);
                    Cri.Add(cs[i]);
                    sum += cs[i].weight;

                    Dictionary<Criteria, double> cx = new Dictionary<Criteria, double>();

                    for (int j = i + 1; j < cs.Count; j++)
                    {
                        lstPair.Items.Add(cs[i].iName + " with " + cs[j].iName);
                        P1.Add(cs[i]);
                        P2.Add(cs[j]);

                        cx.Add(cs[j], 0.0);

                        double px = 1;
                        if (cs[i].weight != 0 && cs[j].weight != 0)
                        {
                            px = cs[i].weight / cs[j].weight;
                        }

                        PX.Add(px);
                    }

                    dicPair.Add(cs[i], cx);
                }

                if (sum > 0)
                {
                    for (int i = 0; i < cs.Count; i++)
                    {
                        WW[cs[i]] = cs[i].weight / sum;
                    }
                }

                lstPair.SelectedIndex = 0;

                CalWight();
            }
            catch
            {
            }
        }

        private void lstPair_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int i = lstPair.SelectedIndex;

                lblP1.Text = P1[i].iName;
                lblP2.Text = P2[i].iName;

                if (PX[i] != 0)
                {
                    if (PX[i] == 1)
                    {
                        pw1.Value = 500;
                        pw2.Value = 500;
                    }
                    else
                    {
                        int p2 = Convert.ToInt32(1000.0 / (PX[i] + 1));
                        if (p2 > 990) p2 = 990;
                        if (p2 < 10) p2 = 10;
                        pw2.Value = p2;
                        pw1.Value = 1000 - pw2.Value;

                    }
                }
                else
                {
                    pw1.Value = 500;
                    pw2.Value = 500;
                }

                lblV1.Text = (pw1.Value / 10.0).ToString();
                lblV2.Text = (pw2.Value / 10.0).ToString();
            }
            catch
            {
            }
        }

        private void pw1_Scroll(object sender, EventArgs e)
        {
            try
            {
                pw2.Value = 1000 - pw1.Value;
                lblV1.Text = (pw1.Value / 10.0).ToString();
                lblV2.Text = (pw2.Value / 10.0).ToString();
                setPX();
            }
            catch
            {
            }
        }

        private void pw2_Scroll(object sender, EventArgs e)
        {
            try
            {
                pw1.Value = 1000 - pw2.Value;
                lblV1.Text = (pw1.Value / 10.0).ToString();
                lblV2.Text = (pw2.Value / 10.0).ToString();
                setPX();
            }
            catch
            {
            }
        }

        private void setPX()
        {
            try
            {
                double ratio = Math.Round((pw1.Value * 1.0) / pw2.Value, 3);
                PX[lstPair.SelectedIndex] = ratio;
                lblRatio.Text = ratio.ToString();
            }
            catch
            {
            }

        }

        private void pw1_MouseCaptureChanged(object sender, EventArgs e)
        {
            try
            {
                CalWight();
            }
            catch
            {
            }
        }

        private void pw2_MouseCaptureChanged(object sender, EventArgs e)
        {
            try
            {
                CalWight();
            }
            catch
            {
            }
        }

        private void CalWight()
        {
            try
            {
                ReMatrix();
                Normalize();
                CalVector();

                lstResult.Items.Clear();
                foreach (Criteria c in Cri)
                {
                    lstResult.Items.Add(c.iName + " (" + WW[c] + ")");
                }
            }
            catch
            {
            }
        }

        private void ReMatrix()
        {
            try
            {
                int count = WW.Count;
                int vec = 0;

                for (int i = 0; i < count; i++)
                {
                    for (int j = 0; j < count; j++)
                    {
                        if (i == j)
                        {
                            Matrix[i, j] = 1;
                        }
                        else if (i < j)
                        {
                            Matrix[i, j] = PX[vec++];
                        }
                        else
                        {
                            Matrix[i, j] = 1 / Matrix[j, i];
                        }
                    }
                }
            }
            catch
            {
            }
        }

        private void Normalize()
        {
            try
            {
                int count = WW.Count;
                size = new double[count];

                for (int j = 0; j < count; j++)
                {
                    double sum = 0;
                    for (int i = 0; i < count; i++)
                    {
                        sum += Matrix[i, j];
                    }

                    if (sum > 0)
                    {
                        for (int i = 0; i < count; i++)
                        {
                            Matrix[i, j] = Matrix[i, j] / sum;
                        }
                    }

                    size[j] = sum > 0 ? sum : 1;
                }
            }
            catch
            {
            }

        }

        private void CalVector()
        {
            try
            {
                int count = WW.Count;
                double[] ww = new double[count];
                double sum = 0;

                double lamda = 0;

                for (int i = 0; i < count; i++)
                {
                    ww[i] = 0;
                    for (int j = 0; j < count; j++)
                    {
                        ww[i] += Matrix[i, j];
                    }
                    sum += ww[i];
                }

                if (sum > 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        WW[Cri[i]] = Math.Round(ww[i] / sum, 3);
                        lamda += WW[Cri[i]] * size[i];
                    }
                }

                double ci = (lamda - count) / (count - 1);
                double cr = ci / RI[count];

                lblCR.Text = Math.Round(cr, 2).ToString();
            }
            catch
            {
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.Dispose();
            }
            catch
            {
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Criteria c in Cri)
                {
                    if (c.ParticipateWeight.ContainsKey(Par))
                    {
                        c.ParticipateWeight[Par] = WW[c];
                    }
                    else
                    {
                        c.ParticipateWeight.Add(Par, WW[c]);
                    }

                    c.Text = c.iName + " (" + WW[c] + ")";
                }

                Tree.Refresh();
                this.Dispose();
            }
            catch
            {
            }
        }

    }
}
